from django.shortcuts import render, HttpResponse

from .myform import Info
from .models import Info as InfoModel


# Create your views here.

def info(request):
    if request.method == "GET":
        # 返回一个渲染的表单
        infoform = Info()
        return render(request, 'info_stu.html', {"infoform": infoform})
    elif request.method == "POST":
        # 获取表单中数据
        infoform = Info(request.POST)
        if infoform.is_valid():  # 检验一下 表单传过来的数据是否合法
            print(infoform.cleaned_data)
            # 拿到数据后  后续操作
            info_model = InfoModel(**infoform.cleaned_data)
            info_model.save()
            return HttpResponse("ok")
        else:
            return render(request, 'info_stu.html', {"infoform": infoform})


# todo  样式
# 升级 modelForm
# image

from django.db.models import Q, F


def testq(request):
    # students = InfoModel.objects.filter(name='Andy').filter(age=99)
    students = InfoModel.objects.filter(Q(name='Andy') | Q(name='北京'))
    # students = InfoModel.objects.filter(~Q(name='Andy'))
    # student = InfoModel.objects.filter(id=1).first()
    # student.age = student.age+1
    # student.save()
    # 在F函数中传入字段名就可以获取字段值
    # 需要在原来的值的基础上进行改变
    student = InfoModel.objects.filter(id=1).update(age=F('age') + 1)
    # student = InfoModel.objects.filter(id=1).update(age=13 + 1)
    print(student)
    return HttpResponse("查询ok")


